﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.MaHaiBo.API.DTOS;
using RBAC.MaHaiBo.Entity;
using RBAC.MaHaiBo.Service;
using System.Data;
using System.Diagnostics.Metrics;
using System.Security.Cryptography;
using MediatR;
using RBAC.MaHaiBo.API.Application.Command.RoleCommand;
using Microsoft.AspNetCore.Authorization;

namespace RBAC.MaHaiBo.API.Controllers
{
    /// <summary>
    /// 角色控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class RoleController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<RoleController> logger;
        /// <summary>
        /// 终结者
        /// </summary>
        IMediator mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志</param>
        ///  <param name="mediator">中介者</param>
        public RoleController(ILogger<RoleController> logger,
        IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加角色信息
        /// </summary>
        /// <param name="request">要添加的角色信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<List<RoleInfo>>> AddRole(RoleCreateCommand request)
        {
            ApiResult<List<RoleInfo>> result = new ApiResult<List<RoleInfo>>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【角色服务层】 添加角色信息" + ex.Message);
                throw;
            }
            return result;
        }

        ///// <summary>
        ///// 批量删除角色信息
        ///// </summary>
        ///// <param name="Ids">要删除的角色编号集合</param>
        ///// <returns>返回受影响行数</returns>
        //[HttpPut]
        //public async Task<ApiResult<List<RoleInfo>>> ByUpdateRole(List<int> Ids)
        //{
        //    ApiResult<List<RoleInfo>> result = new ApiResult<List<RoleInfo>>();
        //    try
        //    {
        //        result.Code = await rSer.ByUpdateRole(Ids);
        //    }
        //    catch (Exception ex)
        //    {
        //        result.Code = -1;
        //        result.Message = ex.Message;
        //        logger.LogError("【角色服务层】 批量删除角色信息" + ex.Message);
        //        throw;
        //    }
        //    return result;
        //}

        /// <summary>
        /// 逻辑删除角色编号
        /// </summary>
        /// <param name="request">角色编号</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<List<RoleInfo>>> DeleteRole([FromQuery] RoleDeleteCommand request)
        {
            ApiResult<List<RoleInfo>> result = new ApiResult<List<RoleInfo>>();
            try
            {
                result.Code = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【角色服务层】 批量修改角色信息" + ex.Message);
                throw;
            }
            return result;
        }

        /// <summary>
        /// 显示全部角色信息
        /// </summary>
        /// <returns>返回角色数据集合</returns>
        [HttpGet]
        public async Task<ApiResult<FenYeDTO<RoleInfo>>> GetRole([FromQuery] RoleShowCommand request)
        {
            ApiResult<FenYeDTO<RoleInfo>> result = new ApiResult<FenYeDTO<RoleInfo>>();
            try
            {
                result.Data = await mediator.Send(request);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【角色服务层】 显示全部角色信息" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填角色数据信息
        /// </summary>
        /// <param name="uid">角色编号</param>
        /// <returns>返回数据信息</returns>
        [HttpGet]
        public async Task<ApiResult <RoleInfo>> GetRoleInfo([FromQuery] RoleBackfillCommand request)
        {
            ApiResult<RoleInfo> result = new ApiResult<RoleInfo>();
            try
            {
                result.Data = await mediator.Send(request);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【角色服务层】 反填角色数据信息" + ex.Message);
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="Role">修改后的数据信息</param>
        /// <returns>返回受影响行数</returns>
        [HttpPut]
        public async Task<ApiResult<List<RoleInfo>>> UpdateRole(RoleUpdateCommand request)
        {
            ApiResult<List<RoleInfo>> result = new ApiResult<List<RoleInfo>>();
            try
            {
                result.Code = await mediator.Send(request);

            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                logger.LogError("【角色服务层】 批量修改角色信息" + ex.Message);
                throw;
            }
            return result;
        }

    }
}
